package com.lenovo.smarttraffic.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.RouteInfoBean;
import com.lenovo.smarttraffic.ui.adapter.MapDetailRecyclerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

/**
 * Created by Caleb.F
 * on 2019-11-23
 * at 16:09
 */
public class MapDetailActivity extends BaseActivity {
    private TextView mTvStart;
    private TextView mTvEnd;
    private TextView mTvTime;
    private TextView mTvMile;
    private RecyclerView mMyMapRecyclerView;
    private  RouteInfoBean mRouteInfoBean;
    private static final String TAG="MapDetailActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode=ThreadMode.MAIN,sticky = true)
    public void getRouteInfo(RouteInfoBean routeInfoBean){
        mRouteInfoBean=routeInfoBean;
        Toast.makeText(this, "跳转成功", Toast.LENGTH_SHORT).show();
        setData();

    }

    private void setData() {

        mTvStart.setText(mRouteInfoBean.getStartSpot());
        mTvEnd.setText(mRouteInfoBean.getEndSpot());
        mTvMile.setText(new StringBuilder().append(Integer.parseInt(mRouteInfoBean.getRoute().getPaths().get(0).getDistance()) / 1000).append("公里"));
        int allseconds=Integer.parseInt(mRouteInfoBean.getRoute().getPaths().get(0).getDuration());
        int hour = Integer.parseInt(mRouteInfoBean.getRoute().getPaths().get(0).getDuration())/3600;
        int minute=(allseconds-hour*3600)/60;
        mTvTime.setText(new StringBuilder().append(hour).append("小时").append(minute).append("分钟"));
        mMyMapRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayout.VERTICAL,false    ));
        MapDetailRecyclerAdapter mapDetailRecyclerAdapter = new MapDetailRecyclerAdapter(this, mRouteInfoBean);
        mMyMapRecyclerView.setAdapter(mapDetailRecyclerAdapter);
    }


    private void initView() {
        mTvStart = findViewById(R.id.tv_start);
        mTvEnd = findViewById(R.id.tv_end);
        mTvTime = findViewById(R.id.tv_time);
        mTvMile = findViewById(R.id.tv_mile);
        mMyMapRecyclerView = findViewById(R.id.my_map_recycler_view);
        initToolBar("路线指南");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_map_detail;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
